package com.jcode.skeleton.web;

import java.util.Date;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jcode.skeleton.model.CRUDEntity;
import com.jcode.skeleton.model.Page;
import com.jcode.skeleton.model.ResponsePacket;
import com.jcode.skeleton.service.GenericService;

/**
 * @Desc
 *
 * @Author dreajay
 * @DateTime 2016年6月4日 下午4:35:39
 * 
 */
public class CrudController<T extends CRUDEntity> extends BaseController {
	protected GenericService service;
	protected String viewPath;
	protected String viewModelName;

	public CrudController() {
	}

	public GenericService getService() {
		return service;
	}

	public void setService(GenericService service) {
		this.service = service;
	}

	@GetMapping("/list")
	@ResponseBody
	public ResponsePacket list(T model) {
		Page page = getService().selectListByPage(model);
		ResponsePacket responsePacket = ResponsePacket.getSuccessResponsePacket();
		responsePacket.setResult(page);
		return responsePacket;
	}

	@GetMapping("/gotoList")
	public ModelAndView listView(T model, ModelAndView modelAndView) {
		Page page = getService().selectListByPage(model);
		modelAndView.addObject("page", page);
		modelAndView.setViewName(getListView());
		return modelAndView;
	}

	@GetMapping("/gotoDetail")
	public ModelAndView gotoDetail(T model, ModelAndView modelAndView) {
		modelAndView.addObject("model", getService().selectByPrimaryKey(model));
		modelAndView.setViewName(getDetailView());
		return modelAndView;
	}

	@GetMapping("/gotoAdd")
	public ModelAndView gotoAdd(T model, ModelAndView modelAndView) {
		modelAndView.setViewName(getAddView());
		return modelAndView;
	}

	@PutMapping("/add")
	@ResponseBody
	public ResponsePacket add(T model) {
		model.setCreateTime(new Date());
		getService().insert(model);
		return ResponsePacket.getSuccessResponsePacket();
	}

	@GetMapping("/gotoUpdate")
	public ModelAndView gotoUpdate(T model, ModelAndView modelAndView) {
		modelAndView.addObject("model", getService().selectByPrimaryKey(model));
		modelAndView.setViewName(getEditView());
		return modelAndView;
	}

	@PostMapping("/update")
	@ResponseBody
	public ResponsePacket update(T model) {
		model.setUpdateTime(new Date());
		getService().updateByPrimaryKey(model);
		return ResponsePacket.getSuccessResponsePacket();
	}

	@DeleteMapping("/delete")
	@ResponseBody
	public ResponsePacket delete(T model) {
		getService().deleteByPrimaryKey(model);
		return ResponsePacket.getSuccessResponsePacket();
	}

	@DeleteMapping("/batchDelete")
	@ResponseBody
	public ResponsePacket batchDelete(String ids) {
		getService().batchDelete(ids.split(","));
		return ResponsePacket.getSuccessResponsePacket();
	}

	@GetMapping("/deleteAndForwardList")
	public ResponsePacket deleteAndForwardList(T model, ModelAndView modelAndView) {
		getService().deleteByPrimaryKey(model);
		modelAndView.setViewName("forward:/" + getViewModelName() + "/list.do");
		return ResponsePacket.getSuccessResponsePacket();
	}

	public String getViewModelName() {
		return viewModelName;
	}

	public void setViewModelName(String viewModelName) {
		this.viewModelName = viewModelName;
	}

	protected String getViewPath() {
		return viewPath;
	}

	protected void setViewPath(String viewPath) {
		this.viewPath = viewPath;
	}

	protected String getView(String view) {
		return String.format("%s/%s_%s", getViewPath(), getViewModelName(), view);
	}

	protected String getListView() {
		return getView("list");
	}

	protected String getDetailView() {
		return getView("detail");
	}

	protected String getAddView() {
		return getView("add");
	}

	protected String getEditView() {
		return getView("edit");
	}
}
